import ScatterJS from 'scatterjs-core';
import ScatterEOS from 'scatterjs-plugin-eosjs';
import Eos from 'eosjs';
import * as config from '@/config';
import EosPriceFormatter from './eosPriceFormatter';

import axios from 'axios';
axios.defaults.withCredentials=true;

const host = "https://api.ydapp.io/"

ScatterJS.plugins(new ScatterEOS());

// @trick: use function to lazy eval Scatter eos, in order to avoid no ID problem.
const eos = () => ScatterJS.scatter.eos(config.network, Eos, { expireInSeconds: 60 });
const currentEOSAccount = () => ScatterJS.scatter.identity.accounts.find(x => x.blockchain === 'eos');

const API = {
  install(Vue) {
    Object.defineProperties(Vue.prototype, {
      $API: {
        get() {
          return API;
        },
      },
    });
  },
  connectScatterAsync() {
    return ScatterJS.scatter.connect(config.appScatterName, { initTimeout: 2000 });
  },
  loginScatterAsync(invite_code) {
    const requiredFields = { accounts: [config.network] };
    return ScatterJS.scatter.getIdentity(requiredFields);
  },
  logoutScatterAsync() {
    return ScatterJS.scatter.forgetIdentity();
  },
  async getPlayerInfoAsync() {
    const { rows } = await eos().getTableRows({
      json: true,
      code: 'luckycat1111',
      scope: 'luckycat1111',
      table: 'player',
      limit: 300,
    });
    return rows;
  },
  transferEOSAsync({
    to,
    memo = '',
    amount = 0,
  }) {
    return eos().transfer(
      currentEOSAccount().name,
      to,
      EosPriceFormatter.formatPrice(amount),
      memo, {
        authorization: [`${currentEOSAccount().name}@${currentEOSAccount().authority}`],
      },
    );
  },
  async transferCard({
    from, to, cardId
  }) {
    const contract = await eos().contract('luckycat1111');
    return contract.transfercard(
      from,
      to,
      cardId,
      { authorization: [`${currentEOSAccount().name}@${currentEOSAccount().authority}`] },
    );
  },
  async claim({
    from 
  }) {
    const contract = await eos().contract('luckycat1111');
    return contract.claim(
      from,
      { authorization: [`${currentEOSAccount().name}@${currentEOSAccount().authority}`] },
    );
  },
  async claimhappy({
    from
  }) {
    const contract = await eos().contract('luckycat1111');
    return contract.claimhappy(
      from,
      { authorization: [`${currentEOSAccount().name}@${currentEOSAccount().authority}`] },
    );
  },
  async claimnew({
    from
  }) {
    const contract = await eos().contract('luckycat1111');
    return contract.claimnew(
      from,
      { authorization: [`${currentEOSAccount().name}@${currentEOSAccount().authority}`] },
    );
  },
  async claimyear({
    from
  }) {
    const contract = await eos().contract('luckycat1111');
    return contract.claimyear(
      from,
      { authorization: [`${currentEOSAccount().name}@${currentEOSAccount().authority}`] },
    );
  },
  async claimhny({
    from
  }) {
    const contract = await eos().contract('luckycat1111');
    return contract.claimhny(
      from,
      { authorization: [`${currentEOSAccount().name}@${currentEOSAccount().authority}`] },
    );
  },
  async registerOrLogin(eos_address, inviter_id) {
    var result = new Object();
    await axios.post(host+'ydapp/register_or_login/', {
      eos_address: eos_address,
      inviter_id: inviter_id
    }).
    then(function (response) {
      result = response.data.stats;
    })
    return result;
  },
  async sendNews(title, content) {
    var result = new Object();
    await axios.post(host+'ydapp/create_news/', {
      title: title,
      content: content
    }).
    then(function (response) {
      result = response.data;
    })
    return result;
  },
  async sendQuestion(content) {
    var result = new Object();
    await axios.post(host+'ydapp/create_question/', {
      question: content
    }).
    then(function (response) {
      result = response.data;
    })
    return result;
  },
  async sendAdvice(title, content) {
    var result = new Object();
    await axios.post(host+'ydapp/create_advice/', {
      title: title,
      content: content
    }).
    then(function (response) {
      result = response.data;
    })
    return result;
  },
  async getMyInfo() {
    var result = new Object();
    await axios.get(host+'ydapp/get_my_info/').
    then(function (response) {
      result = response.data.stats;
    })
    return result;
  },
  async logoutAsync() {
    var result = new Object();
    await axios.post(host+'ydapp/logout/', {
    }).
    then(function (response) {
      result = response.data;
    })
    return result;
  },
  async getGameList() {
    var result = new Object();
    await axios.get(host+'ydapp/game_list/').
    then(function (response) {
      result = response.data.result;
    })
    return result;
  },
  async getNewsList() {
    var result = new Object();
    await axios.get(host+'ydapp/news_list/').
    then(function (response) {
      result = response.data.news;
    })
    return result;
  },
  async getQuestionList() {
    var result = new Object();
    await axios.get(host+'ydapp/question_list/').
    then(function (response) {
      result = response.data.questiones;
    })
    return result;
  },
  async getAdviceList() {
    var result = new Object();
    await axios.get(host+'ydapp/advice_list/').
    then(function (response) {
      result = response.data.advices;
    })
    return result;
  },
  async updateMyInfo(nickname, trx_address, avatar_url) {
      var result = new Object();
      await axios.post(host+"ydapp/update_my_info/", {
        nickname: nickname,
        avatar_url: avatar_url,
        trx_address: trx_address 
      })
      .then(function(response) {
        result = response.data;
      })
      return result;
    },
  async sendNewsComment(new_id, content) {
    var result = new Object();
    await axios.post(host+"ydapp/send_news_comment/", {
      new_id: new_id,
      content: content
    })
    .then(function(response) {
      result = response.data;
    })
    return result;
  },
  async toggleNewsLike(new_id) {
    var result = new Object();
    await axios.post(host+"ydapp/like_news/", {
      new_id: new_id,
    })
    .then(function(response) {
      result = response.data;
    })
    return result;
  },
  async doSignIn(answer) {
    var result = new Object();
    await axios.post(host+"ydapp/sign_in/", {
      answer: answer,
    })
    .then(function(response) {
      result = response.data;
    })
    return result;
  },
  async getCpuInfo(answer) {
    var result = new Object();
    await axios.get(host+"ydapp/cpu/")
    .then(function(response) {
      result = response.data;
    })
    return result;
  },
};

export default API;
export { eos, currentEOSAccount };
